#region Copyright(C) M.Shams Mukhtar (shams_mukhtar@yahoo.com)
//
// You are free to use or modify the code, as long as you place
// copyright notice above. Thanks!
//
// Filename: PageControlBase.cs
#endregion

#region Instructions
#endregion

#region Remarks
#endregion

#region Namespaces used
	using System;
	using System.Web.UI;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;
#endregion

namespace Shams.Web.UI.MasterPages
{
	/// <summary>
	/// Summary description for PageControlBase.
	/// </summary>
	public abstract class PageControlBase : Control, IPageControl, INamingContainer
	{
		protected string pageTitle;
		protected string metaInfo;
		protected string styleSheetLink;

		protected HtmlForm htmlForm = new HtmlForm();

		public PageControlBase()
		{
			pageTitle = "Welcome";
			metaInfo =	@"<meta name='GENERATOR' Content='Microsoft Visual Studio .NET 7.1'>
						<meta name='CODE_LANGUAGE' Content='C#'>
						<meta name=vs_defaultClientScript content='JavaScript'>
						<meta name=vs_targetSchema content='http://schemas.microsoft.com/intellisense/ie5'>
						";
			styleSheetLink=	@"<LINK href='StyleSheet/Style.css' type='text/css' rel='stylesheet'>";

			htmlForm.ID = "PageControlBaseForm" + htmlForm.GetHashCode().ToString();
		}

		public static void TestDriver(HtmlTextWriter writer)
		{
			PageControlBase pcb = MasterPageFactory.CreateMasterPageControl();
			pcb.PageRender();
			
			System.Diagnostics.Debug.WriteLine( writer.ToString() );
		}

		public System.Web.UI.Control ParentControl()
		{
			return this;
		}

		public void PageRender()
		{
			HtmlRenderStarts();
			PreHtmlRender();
			HtmlRender();
			PostHtmlRender();
			HtmlRenderEnds();
		}

		public virtual void HtmlRenderStarts()
		{
			this.Controls.AddAt(0, new LiteralControl(@"<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.0 Transitional//EN'>
				<html>
					<head>
						<title>" + PageTitle + @"</title>" + 
				metaInfo + styleSheetLink + @" 
					" + 
				@"</head>
					<body MS_POSITIONING='GridLayout'>
					" ));
		}

		public virtual void PreHtmlRender()
		{
			// AddTestControl();
		}

		private void AddTestControl()
		{
			this.Controls.Add( new LiteralControl( @"
						<table bgcolor='silver' width='100%'>
							<tr>
								<td colspan=2><h1>Our Page Header</h1></td>
							</tr>
						</table>
						") );
		}

		protected override void CreateChildControls() 
		{
			PageRender();
		}

		public virtual void HtmlRender()
		{
			this.Controls.Add(htmlForm);
		}

		public virtual void PostHtmlRender()
		{

		}

		public virtual void HtmlRenderEnds()
		{
			this.Controls.Add(new LiteralControl( @"
					</body>
				</html>") );
		}

		protected override void OnPreRender(EventArgs e)
		{
		}

		protected override void Render(HtmlTextWriter writer)
		{
			base.Render(writer);
		}

		public void AddToHtmlForm(Control childControl)
		{
			htmlForm.Controls.Add(childControl);
		}

		public void AddMetaInfo(string meta_info)
		{
			metaInfo += meta_info;
		}

		public virtual string PageTitle
		{
			get
			{
				string text = (string) ViewState["PageTitle"];
				return (text == null) ? pageTitle : text;
			}
			set
			{
				ViewState["PageTitle"] = value;
			}
		}

		public virtual string MetaInfo
		{
			get
			{
				string text = (string) ViewState["MetaInfo"];
				return (text == null) ? metaInfo : text;
			}
			set
			{
				ViewState["MetaInfo"] = value;
			}
		}

		public virtual string StyleSheetLink
		{
			get
			{
				string text = (string) ViewState["StyleSheetLink"];
				return (text == null) ? styleSheetLink : text;
			}
			set
			{
				ViewState["StyleSheetLink"] = value;
			}
		}
		
		public HtmlForm HtmlPageForm
		{
			get
			{
				return htmlForm;
			}
			set
			{
				htmlForm = value;
			}
		}
	}
}
